using System.Xml.Linq;
using TDUnit.Common.TestData;

namespace TDUnit.TestDataParsers.XmlParser
{
    public class TestDataElementsProcessor : ElementProcessorBase, IXmlTestDataProcessor
    {
        private readonly TestData testData;

        public TestDataElementsProcessor(TestData testData)
        {
            this.testData = testData;
        }

        public void Process(XDocument document)
        {
            AssertRootElementIsValid(document);

            // ReSharper disable PossibleNullReferenceException
            //AssertRootElementIsValid(document) checks if document.Root is null.
            var ns = document.Root.GetDefaultNamespace();
            // ReSharper restore PossibleNullReferenceException

            var elementParsers = new TableElementParser(new RowElementParser(new ColumnElementParser()));
            elementParsers.Parse(document.Root.Elements(ns + "Table"), testData);
        }

    }
}